/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package encaps.security;

import encaps.action.*;
import encaps.entities.Role;


public class SecuritySystem {

    private RoleCache cache;

    public SecuritySystem() {
        try {
            cache = new RoleCache();
        } catch (DataAccessException ex) {
            ex.printStackTrace();
        }
    }

    public boolean isActionAllowed(String roleName, Action action) throws DataAccessException {
        Role userRole = getRole(roleName);
        return userRole.hasGrant(action.getRequiredGrant());
    }

    private Role getRole(String roleName) {
        try {
            return cache.get(roleName);
        } catch (DataAccessException dataAccessException) {
            dataAccessException.printStackTrace();
            return null;
        }
    }
}
